/**
 * 
 */
package co.fitstart.entity.content.page;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Where;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * 页面
 * 
 * @author Leo
 * @author lute
 *
 */
@Entity
@Table(name="content_webpage")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Webpage {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 页面名称 */
    private String name;
    
    /** 页面ID */
    private String alias;
    
    private String modules;
    
    @OneToMany(mappedBy = "webpage", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @MapKey(name = "alias")
    @Where(clause = "is_disabled = 0")
    private List<Link> links;
    
    @OneToMany(mappedBy = "webpage", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Brief> briefs;
    
    private Date createTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Webpage() {

    }
    
    /**
     * 
     * @param alias
     * @return
     */
    public boolean matches(String alias) {
        return StringUtils.equalsIgnoreCase(alias, this.alias);
    }
    
    /**
     * 
     * @return
     */
    @JSONField(serialize = false)
    public String[] getModuleAlias() {
        
        if(StringUtils.isNoneBlank(this.modules)) {
            return StringUtils.split(this.modules, ",");
        }
        
        return null; 
    }
    
    /**
     * edit webpage name
     * 
     * @param name
     * @return
     */
    public Webpage edit(String name) {
        this.name = name;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the alias
     */
    public String getAlias() {
        return alias;
    }

    /**
     * @return the modules
     */
    public String getModules() {
        return modules;
    }
    
    /**
     * @return the links
     */
    public List<Link> getLinks() {
        return links;
    }
    
    /**
     * @return the briefs
     */
    public List<Brief> getBriefs() {
        return briefs;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param alias the alias to set
     */
    public void setAlias(String alias) {
        this.alias = alias;
    }

    /**
     * @param modules the modules to set
     */
    public void setModules(String modules) {
        this.modules = modules;
    }
    
    /**
     * @param links the links to set
     */
    public void setLinks(List<Link> links) {
        this.links = links;
    }
    
    /**
     * @param briefs the briefs to set
     */
    public void setBriefs(List<Brief> briefs) {
        this.briefs = briefs;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}
